﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;


//角色管理
public class RoleMgr : Singleton<RoleMgr>
{
    int _mainRoleThisID;
    //ThisID,Role
    public Dictionary<int, Role> AllRole = new Dictionary<int, Role>();
  
    internal static void OnMainRoleThisID(Cmd cmd)
    {
        if (!Net.CheckCmd(cmd, typeof(MainRoleThisIDCmd))){ return; };
        MainRoleThisIDCmd mainRoleThisIDCmd = cmd as MainRoleThisIDCmd;
        RoleMgr.instance._mainRoleThisID = mainRoleThisIDCmd.ThisID;
    }

    internal static void OnCreateSceneRole(Cmd cmd)
    {
        if (!Net.CheckCmd(cmd, typeof(CreateSceneRoleCmd))) { return; };
        CreateSceneRoleCmd createSceneRoleCmd = cmd as CreateSceneRoleCmd;

        RoleDataBase roleDataBase = RoleTable.instance[createSceneRoleCmd.ModleID];
        if (roleDataBase==null)
        {
            Debug.LogError("未找到角色模型：" + createSceneRoleCmd.ModleID);
            return;
        }
        //创建角色模型
        var roleObj= ResMgr.instance.GetInstance(roleDataBase.ModelPath);
        Role role;
        //判断是否是主角
        if (instance._mainRoleThisID==createSceneRoleCmd.ThisID)
        {
            //主角
            role = roleObj.AddComponent<MainRole>();

        }
        else
        {
             role = roleObj.AddComponent<Role>();
        }
        
        role.Init(createSceneRoleCmd, roleDataBase);

        RoleMgr.instance.AllRole[createSceneRoleCmd.ThisID] = role;
         


    }

}
